﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Daemon.Common.Auth
{
    public class AppJwtBearerEvents : JwtBearerEvents
    {
        public override Task MessageReceived(MessageReceivedContext context)
        {
            string authorization = context.Request.Headers[HeaderNames.Authorization];

            // if (string.IsNullOrEmpty(authorization))
            // {
            //     context.NoResult();
            //     return Task.CompletedTask;
            // }

            // if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
            // {
            //     context.Token = authorization["Bearer ".Length..].Trim();
            // }

            // if (string.IsNullOrEmpty(context.Token))
            // {
            //     context.NoResult();
            //     return Task.CompletedTask;
            // }

            return Task.CompletedTask;
        }

        public override Task TokenValidated(TokenValidatedContext context)
        {
            return Task.CompletedTask;
        }

        public override Task AuthenticationFailed(AuthenticationFailedContext context)
        {
            Console.WriteLine($"Exception: {context.Exception}");

            return Task.CompletedTask;
        }

        public override Task Challenge(JwtBearerChallengeContext context)
        {
            context.HandleResponse();

            return Task.CompletedTask;
        }

        public override Task Forbidden(ForbiddenContext context)
        {
            return Task.CompletedTask;
        }
    }

}
